﻿package app.view.components.guiDima.switchContent 
{
	import app.view.components.guiDima.NavigatorPage;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import gs.TweenLite;
	/**
	 * ...
	 * @author www
	 */
	public class SwitchContentDBase extends Sprite
	{
		protected var _data:Array;
		protected var _currentPageID:int = 0;
		protected var _currentSection:ContentBaseD;
		protected var _contentWidth:Number;
		protected var _contentHeight:Number;
		private var _narrowLast:NarrowD;
		private var _narrowNext:NarrowD;
		
		private var _isLast:Boolean;
		private var _isNext:Boolean;
		
		//public static var _gapNarrow:Number = 2;
		private var _gapNarrow:Number = 2;
		public static var GAP_V_NAVIGATOR_PAGE:Number = 10;
		private static const TIME_SHOW_NAVIGATE:Number = 0.3;
		private static const TIME_HIDE_NAVIGATE:Number = 0.2;
		//private var _navigatorPage:NavigatorPage;
		private var _isNavigatorPage:Boolean;
		
		
		public function SwitchContentDBase() 
		{
			//super();
			
			init();
		}
		
		public function init():void
		{
			////trace('www!!!',_contentWidth);
			
			_narrowLast = new NarrowD();
			_narrowLast.selected = true;
			_narrowLast.alpha = 0;
			addChild(_narrowLast);
			_narrowLast.x = -_gapNarrow;
			_narrowLast.y = _contentHeight / 2;
			
			_narrowNext = new NarrowD();
			_narrowNext.rotation = 180;
			_narrowNext.selected = true;
			_narrowNext.alpha = 0;
			addChild(_narrowNext);
			_narrowNext.x = _contentWidth + _gapNarrow;
			_narrowNext.y = _contentHeight / 2;
			
			_narrowNext.doubleClickEnabled = true;
			_narrowLast.doubleClickEnabled = true;
			
			//_navigatorPage = new NavigatorPage();
			//addChild(_navigatorPage);
			//isNavigatorPage = false;
			//_navigatorPage.x = _contentWidth;
			//_navigatorPage.y = _contentHeight+GAP_V_NAVIGATOR_PAGE;
		}
		
		private function setEvent():void
		{
			_narrowLast.addEventListener(MouseEvent.CLICK, onCLickLast);
			_narrowNext.addEventListener(MouseEvent.CLICK, onCLickNext);
			//_navigatorPage.addEventListener(Event.CHANGE, onChangeNavigatorPage);
		}
		
		private function removeEvent():void
		{
			_narrowLast.removeEventListener(MouseEvent.CLICK, onCLickLast);
			_narrowNext.removeEventListener(MouseEvent.CLICK, onCLickNext);
			//_navigatorPage.removeEventListener(Event.CHANGE, onChangeNavigatorPage);
			
			_narrowLast.selected = true;
			_narrowLast.alpha = 0;
			
			_narrowNext.selected = true;
			_narrowNext.alpha = 0;
		}
		
		public function destroy():void
		{
			removeEvent();
			
			try 
			{
				removeChild(_narrowLast);
				removeChild(_narrowNext);
				
				//_navigatorPage.destroy();
				//removeChild(_navigatorPage);
			}catch (err:Error)
			{
				trace('e ');
			}
			
			
		}
		
		public function change():void 
		{
			////trace('change SwitchContentDBase: ');
			
			/*if ( _currentSection ) 
			{
				_oldData = _currentSection.data as SectionVO;
				_currentSection.destroy();
			};
			
			_currentSection = getSectionInstance(sectionVO.id);
			if ( !_currentSection ) return;
			// инициализируем
			_currentSection.alpha = 0;
			_currentSection.open();
			_currentSection.data = sectionVO;
			addChild(_currentSection);*/
		};
		
		public function get data():Array { return _data; }
		
		public function set data(value:Array):void 
		{
			_data = value;
			
			_currentPageID = 0;
			
			if (_data!=null) 
			{
				trace('555',_data.length);
				
				if (_data.length>1) 
				{
					setEvent();
					//_navigatorPage.countPage = _data.length;
					//_navigatorPage.x = _contentWidth - _navigatorPage.width;
					//_navigatorPage.setSelected(_currentPageID);
					// проверим нужно ли включить кнопки
					
					checkedNavigation();
				}else
				{
					removeEvent();
				}
			}
			
		}
		
		private function checkedNavigation():void
		{
			// проверяем навигацию
			// проверка
			////trace('checkedNavigation---+++',_currentPageID);
			if (_currentPageID>=1) 
			{
				TweenLite.to(_narrowLast, TIME_HIDE_NAVIGATE, { alpha:1 } );
				_narrowLast.addEventListener(MouseEvent.CLICK, onCLickLast);
				_isLast = true;
				_narrowLast.selected = !_isLast;
			}else
			{
				_narrowLast.removeEventListener(MouseEvent.CLICK, onCLickLast);
				_isLast = false;
				_narrowLast.selected = !_isLast;
				TweenLite.to(_narrowLast, TIME_HIDE_NAVIGATE, { alpha:0 } );
			}
			
			if (_currentPageID<_data.length-1) 
			{
				TweenLite.to(_narrowNext, TIME_HIDE_NAVIGATE, { alpha:1 } );
				_narrowNext.addEventListener(MouseEvent.CLICK, onCLickNext);
				_isNext = true;
				_narrowNext.selected = !_isNext;
			}else
			{
				_narrowNext.removeEventListener(MouseEvent.CLICK, onCLickNext);
				_isNext = false;
				_narrowNext.selected = !_isNext;
				TweenLite.to(_narrowNext, TIME_HIDE_NAVIGATE, { alpha:0 } );
			}
		}
		
		public function get currentPageID():int { return _currentPageID; }
		
		public function set currentPageID(value:int):void 
		{
			_currentPageID = value;
			
		}
		
		public function get isNavigatorPage():Boolean { return _isNavigatorPage; }
		
		public function set isNavigatorPage(value:Boolean):void 
		{
			_isNavigatorPage = value;
			
			if (_isNavigatorPage==true) 
			{
				//_navigatorPage.visible = true;
			}else
			{
				//_navigatorPage.visible = false;
			}
		}
		
		public function get gapNarrow():Number { return _gapNarrow; }
		
		public function set gapNarrow(value:Number):void 
		{
			_gapNarrow = value;
			
			_narrowLast.x = -_gapNarrow;
			_narrowNext.x = _contentWidth + _gapNarrow;
		}
		
		private function onCLickLast(e:MouseEvent):void 
		{
			_currentPageID = _currentPageID - 1;
			////trace('onCLickLast',_currentPageID,_data.length);
			change();
			
			//_navigatorPage.setSelected(_currentPageID);
			
			// проверка
			if (_currentPageID>=1) 
			{
				_narrowLast.addEventListener(MouseEvent.CLICK, onCLickLast);
				_isLast = true;
				_narrowLast.selected = !_isLast;
				TweenLite.to(_narrowLast, TIME_HIDE_NAVIGATE, { alpha:1 } );
			}else
			{
				_narrowLast.removeEventListener(MouseEvent.CLICK, onCLickLast);
				_isLast = false;
				_narrowLast.selected = !_isLast;
				TweenLite.to(_narrowLast, TIME_HIDE_NAVIGATE, { alpha:0 } );
			}
			
			if (_currentPageID<_data.length-1) 
			{
				_isNext = true;
				_narrowNext.selected = !_isNext;
				TweenLite.to(_narrowNext, TIME_HIDE_NAVIGATE, { alpha:1 } );
				_narrowNext.addEventListener(MouseEvent.CLICK, onCLickNext);
			}
		}
		
		private function onCLickNext(e:MouseEvent):void 
		{
			////trace('onCLickNext',_currentPageID);
			
			_currentPageID = _currentPageID + 1;
			
			change();
			
			//_navigatorPage.setSelected(_currentPageID);
			
			// проверка
			if (_currentPageID<_data.length-1) 
			{
				_narrowNext.addEventListener(MouseEvent.CLICK, onCLickNext);
				_isNext = true;
				_narrowNext.selected = !_isNext;
				TweenLite.to(_narrowNext, TIME_HIDE_NAVIGATE, { alpha:1 } );
			}else
			{
				_narrowNext.removeEventListener(MouseEvent.CLICK, onCLickNext);
				_isNext = false;
				_narrowNext.selected = !_isNext;
				TweenLite.to(_narrowNext, TIME_HIDE_NAVIGATE, { alpha:0 } );
			}
			
			if (_currentPageID>=1) 
			{
				_isLast = true;
				_narrowLast.selected = !_isLast;
				TweenLite.to(_narrowLast, TIME_HIDE_NAVIGATE, { alpha:1 } );
				_narrowLast.addEventListener(MouseEvent.CLICK, onCLickLast);
			}
		}
		
		private function onChangeNavigatorPage(e:Event):void 
		{
			////trace('666', _navigatorPage.current.id);
			//_currentPageID = _navigatorPage.current.id;
			
			change();
			
			checkedNavigation();
		}
		
	}

}